'''
test.py - Unit and integration tests.

TODO:description

Authors: Martin Norbury,
         Eric Saunders

October 2011
'''

from django.test import TestCase
from django.test.client import Client
import json

class ClientIntegrationTest(TestCase):
    def setUp(self):
        self.client = Client()

    def test_get_square_of_two(self):
        """
        Tests square of 2 return 4 using GET.
        """
        # Send get request
        response = self.client.get('/square/2')

        # Convert response content into json dictionary
        result   = json.loads(response.content)

        # Check the result
        self.assertEqual(result['message'], 4)

    def test_get_with_trailing_slash(self):
        """
        Tests a GET request with a trailing slash.
        """
        # Send get request
        response = self.client.get('/square/2/')

        # Convert response content into json dictionary
        result   = json.loads(response.content)

        # Check the result
        self.assertEqual(result['message'], 4)

    def test_post_square_of_two(self):
        """
        Tests square of 2 return 4 using POST.
        """
        # Create POST data
        post_data = self._create_post_data(2)

        # Send POST request
        response = self.client.post('/square/',data=post_data)

        # Convert response content into json dictionary
        result   = json.loads(response.content)

        # Check result
        self.assertEqual(result['message'], 4)

    def _create_post_data(self, *args, **kwargs):
        ''' 
        Create a POST data dictionary.

        The outer dictionary is for transport purposes. The inner dictionary
        contains args and kwargs parameters stored as a serialized json 
        string.
        '''
        return { 'data' : json.dumps( {'args' : args, 'kwargs' : kwargs} ) }
